using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using Net.Common;
using UnityEngine;
using UnityEngine.UI;

/****************************************************
// 功能：战斗结算界面
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/4/10 11:43:16
*****************************************************/

public class ResultWindow : BaseWindow
{
    public Image imgResult;
    public Image imgPrg;
    public Text txtTime;

    protected override void InitWindows()
    {
        base.InitWindows();

        BattleController.Instance.resultWindows = this;
    }

    protected override void ClearWindows()
    {
        base.ClearWindows();

        BattleController.Instance.resultWindows = null;
        GameRoot.Instance.timerList.Clear();
    }

    public void SetBattleResult(bool isSuccess) {
        if(isSuccess) {
            audioManager.PlayUIAudio("victory");
            SetSprite(imgResult, "Images/ResultWnd/win");
        }
        else {
            audioManager.PlayUIAudio("defeat");
            SetSprite(imgResult, "Images/ResultWnd/lose");
        }
        imgResult.SetNativeSize();

        var timer = new MonoTimer(Constants.NUM_FIVE)
        {
            OnTimeUpdated = seconds =>
            {
                imgPrg.fillAmount = seconds / Constants.NUM_FIVE;
                SetText(txtTime, Mathf.CeilToInt(seconds).ToString());
                
            },
            OnTimeCompleted = OnTimeCompleted
        };
        timer.Start();
        
        GameRoot.Instance.AddMonoTimer(timer);
    }

    public void OnTimeCompleted()
    {
        if (!gameObject.activeSelf) return;
        SendByTcp(RequestType.BATTLE_END, new BattleRequest()
        {
            roomId = model.roomId,
            type = (int) RequestType.BATTLE_END
        });
            
        SendByKcp(RequestType.BATTLE_END, new BattleRequest()
        {
            roomId = model.roomId,
            type = (int) RequestType.BATTLE_END
        });
            
        SendByWebSocket(RequestType.BATTLE_END, new BattleRequest()
        {
            roomId = model.roomId,
            type = (int) RequestType.BATTLE_END
        });
    }
    
}
